﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace ElectricDim
{
	public partial class DlgCableTrayDim : System.Windows.Forms.Form
	{
		public static DlgCableTrayDim GetInstance()
		{
			if (DlgCableTrayDim.dco == null || DlgCableTrayDim.dco.IsDisposed)
			{
				DlgCableTrayDim.dco = new DlgCableTrayDim();
			}
			return DlgCableTrayDim.dco;
		}

		public DlgCableTrayDim()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
		}

		private void CableTrayDimDlg_Load(object sender, EventArgs e)
		{
			this.RefreshDataGridViewColIdx();
			this.ReadFormData();
		}

		private void ReadFormData()
		{
			string path = Path.GetTempPath() + "CableTrayDim.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = 0;
			int y = 0;
			try
			{
				x = Convert.ToInt32(streamReader.ReadLine());
				y = Convert.ToInt32(streamReader.ReadLine());
			}
			catch
			{
				x = 0;
				y = 0;
			}
			streamReader.Close();
			fileStream.Close();
			Point location = new Point(x, y);
			base.Location = location;
		}

		private void WriteFormData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "CableTrayDim.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Add();
			this.RefreshDataGridViewColIdx();
		}

		private void button5_Click(object sender, EventArgs e)
		{
			DataGridViewRow currentRow = this.dataGridView1.CurrentRow;
			if (currentRow != null)
			{
				this.dataGridView1.Rows.Remove(currentRow);
				this.RefreshDataGridViewColIdx();
			}
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.RefreshDataGridViewColIdx();
		}

		private void RefreshDataGridViewColIdx()
		{
			DataGridViewRowCollection rows = this.dataGridView1.Rows;
			int count = rows.Count;
			for (int i = 0; i < count; i++)
			{
				rows[i].Cells[0].Value = i + 1;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.objCfg.m_DimStyle = LeaderStyle.None;
			this.objCfg.m_LinkLineStyle = PointStyle.None;
			this.objCfg.m_lstItem.Clear();
			if (this.rbHasLeader.Checked)
			{
				this.objCfg.m_DimStyle = LeaderStyle.HasLeader;
			}
			else if (this.rbOrtLeader.Checked)
			{
				this.objCfg.m_DimStyle = LeaderStyle.Orthogonal;
			}
			if (this.radioButton5.Checked)
			{
				this.objCfg.m_LinkLineStyle = PointStyle.Arrow;
			}
			else if (this.radioButton6.Checked)
			{
				this.objCfg.m_LinkLineStyle = PointStyle.Circle;
			}
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				DataGridViewCell dataGridViewCell = this.dataGridView1.Rows[i].Cells[1];
				if (dataGridViewCell.Value != null)
				{
					this.objCfg.m_lstItem.Add(dataGridViewCell.Value.ToString());
				}
			}
			if (this.objCfg.m_lstItem.Count <= 0)
			{
				MessageBox.Show("标注内容不能为空。");
				return;
			}
			this.OK = true;
			base.DialogResult = DialogResult.OK;
			this.WriteFormData();
		}

		public DlgCableTrayDim.CUserConfig GetConfigData()
		{
			return this.objCfg;
		}

		private void DlgCableTrayDim_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;
			base.Hide();
			this.WriteFormData();
		}

		private void rbNullLeader_CheckedChanged(object sender, EventArgs e)
		{
			this.groupBox2.Enabled = !this.rbNullLeader.Checked;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.OK = false;
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == 274 && (int)m.WParam == 61536)
			{
				this.OK = false;
			}
			base.WndProc(ref m);
		}

		private const int colIdxID = 0;

		private const int colIdxItem = 1;

		private static DlgCableTrayDim dco;

		private DlgCableTrayDim.CUserConfig objCfg = new DlgCableTrayDim.CUserConfig();

		public bool OK;

		public class CUserConfig
		{
			public bool IsValid()
			{
				return this.m_lstItem.Count<string>() > 0;
			}

			public LeaderStyle m_DimStyle;

			public PointStyle m_LinkLineStyle;

			public List<string> m_lstItem = new List<string>();
		}
	}
}
